//封装购物车模块
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { useUserStore } from './userStore'
import { insertCart, findNewCartListAPI, delCartAPI } from '@/apis/cart'
import { mergeCartAPI } from '@/apis/cart'


export const useCartStore = defineStore('cart', (count) => {

  const userStore = useUserStore()
  const isLogin = computed(() => userStore.userInfo.token)

  //1,定义state 
  const cartList = ref([])

  //2,定义action

  //获取最新购物车列表action
  const newList = async () => {
    const res = await findNewCartListAPI() //获取最新购物车列表
    cartList.value = res.result //覆盖本地购物车列表
  }

  //添加购物车
  const addCart = async (goods) => {
    const { skuId, count } = goods
    if (isLogin.value) {
      //登录之后的加入购物车逻辑
      await insertCart({ skuId, count }) //加入购物车接口调用
      newList()
    } else {
      //本地存储的逻辑
      //匹配传递过来的商品对象中的skuId能不能在cartList中找到，找到就是已添加过
      const item = cartList.value.find((item) => goods.skuId === item.skuId)
      // console.log('item', item);
      if (item) {
        //已添加过 -count增加
        item.count += goods.count
      } else {
        //未添加过 -直接push
        cartList.value.push(goods)
      }
    }
  }

  //删除购物车
  const delCart = async (skuId) => {
    if (isLogin.value) {
      //调用接口实现接口购物车中的删除功能
      await delCartAPI([skuId]) //删除购物车接口调用
      newList()
    } else {
      //找到要删除项的下标值 --splice
      const idx = cartList.value.findIndex((item) => skuId === item.skuId)
      cartList.value.splice(idx, 1)

      //使用数组的过滤方法 --filter
    }

  }
  //合并购物车
  const mergeCart = async () => {
    await mergeCartAPI(cartList.value.map(item => {
      return {
        skuId: item.skuId,
        selected: item.selected,
        count: item.count
      }
    }))
    newList()
  }

  //清除购物车
  const clearCart = () => {
    cartList.value = []
  }

  //单选功能
  const singleCheck = (skuId, selected) => {
    //通过skuId找到要修改的那一项 然后把它的selected修改为传递过来的selected
    const item = cartList.value.find((item) => item.skuId === skuId)
    item.selected = selected
  }

  //全选功能
  const allCheck = (selected) => {
    //把cartlist中的每一项的selected都设置为当前全选框的状态
    cartList.value.forEach(item => item.selected = selected)
  }

  //计算属性
  //1，总的数量
  const allCount = computed(() => cartList.value.reduce((a, c) => a + c.count, 0))
  //2，总价
  const allPrice = computed(() => cartList.value.reduce((a, c) => a + c.price * c.count, 0))

  //3，已选商品数量
  //filter返回的仍是数组
  const selectedCount = computed(() => cartList.value.filter(item => item.selected).reduce((a, c) => a + c.count, 0))
  //4，已选商品总价
  const selectedPrice = computed(() => cartList.value.filter(item => item.selected).reduce((a, c) => a + c.count * c.price, 0))

  //是否全选
  //very 方法会对 cartList.value 数组中的每个元素执行提供的函数，判断是否所有元素的 selected 属性都为真，如果是，则返回 true，否则返回 false。
  const isAll = computed(() => cartList.value.every((item) => item.selected))



  return {
    cartList,
    allCount,
    allPrice,
    isAll,
    selectedCount,
    selectedPrice,
    newList,
    addCart,
    delCart,
    singleCheck,
    allCheck,
    clearCart,
    mergeCart
  }
},
  {
    //持久化配置，存入localstorage
    persist: true,
  },
)